
var is_add = true;//定义是否新增变量
var dataType = "";//定义当前的数据类型变量
var demoTableKey = "";

//格式化标准日期
function format(time, format) {
    var t = new Date(time);
    var tf = function(i) {
        return(i < 10 ? '0' : '') + i
    };
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
        switch(a) {
            case 'yyyy':
                return tf(t.getFullYear());
                break;
            case 'MM':
                return tf(t.getMonth() + 1);
                break;
            case 'mm':
                return tf(t.getMinutes());
                break;
            case 'dd':
                return tf(t.getDate());
                break;
            case 'HH':
                return tf(t.getHours());
                break;
            case 'ss':
                return tf(t.getSeconds());
                break;
        }
    })
}

var vanytree = new Vue({
    el: "#treeLeft",
    data: function() {
        return {
            filterText: "",
            title: "",
            icon: "",
            rightIcon: "",
            contexts: "",
            justLeft: true,
            defaultExpandedKeys: [],
            data: [],
            defaultProps: {
                children: '',
                label: 'tableName',
                id: 'tableCode'
            }
        }
    },
    methods: {
        getData: function(params) {
            VanyAjax("/demo/demotable/queryTree",{},"GET").done(function(data) {
                this.defaultExpandedKeys = data.map(function(item) {
                    return item.tableCode;
                });
                this.data = data;
            }, false, this);
        },
        isEmptyTree: function() {
            return this.data.length === 0;
        },
        filterNode: function(value, data) {
            if(!value) return true;
            //return data.label.indexOf(value) !== -1;
            return data[this.defaultProps.label].indexOf(value) !== -1;
        },
        handleNodeClick: function(data, node, nodeComponent) {
        	dataType = data.tableCode;
        	sm.search();
        },
        // TODO
        resizeTree: function() {

        },
        handleCommand: function(command) {
            
        },
        rightIconClick: function() {
            
        },
        hasPre: function(nodeObj, nodeData) {
            
        },
        hasNext: function(nodeObj, nodeData) {
            
        },
        showContexts: function(isShow) {
           
        },
        renderContent:function(){
        	
        }
    },
    mounted: function() {
        /*this.$el.onmouseover = function(e) {
        	console.log(e);
        }*/
    },
    watch: {
        filterText: function(val) {
            this.$refs.tree.filter(val);
        }
    }
});
vanytree.getData();

var dialogInfos = new Vue({
	el: "#dialogInfos",
	data: function() {
		var colOne = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入列一'));
			}
			return callback();
		};
		var colTwo = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入列二'));
			}
			return callback();
		};
		var colThr = function(rule, value, callback) {
			if(!value) {
				return callback(new Error('请输入列三'));
			}
			return callback();
		};
		return {
			dialogVisible:true,
			disabledInput:true,
			tableForm:{
				dataType:"",
				colOne:"",
				colTwo:"",
				colThr:""
			},
			rules: {
				colOne: [{
					required: true,
					validator: colOne,
					trigger: 'blur'
				}],
				colTwo: [{
					required: true,
					validator: colTwo,
					trigger: 'blur'
				}],
				colThr: [{
					required: true,
					validator: colThr,
					trigger: 'blur'
				}]
			},
		};
	},
	methods: {
		submitForm: function(formName) {
			var self = this;
			var params = self.tableForm;
			params['dataType']= dataType;
			var url = "";
			if(is_add){
				url = "/demo/demotable/add";
			}else{
				url = "/demo/demotable/update";
				params['demoTableKey']= demoTableKey;
			}
			VanyAjax(url, params, "GET").done(function (data) { 
				sm.getData(sm.pageIndex,sm.pageSize,sm.keyword);
    			this.dialogVisible = false;
    	    }, true, this);
		},
		resetForm: function(formName) {
			this.$refs[formName].resetFields();
		}, //重置
		cancelForm: function(formName) {
			this.dialogVisible = false;
		},
		handleClose:function(){
			this.dialogVisible = false;
		}
	}
});
dialogInfos.dialogVisible = false;


var msg = [];
var sm = new Vue({
    el: "#tableR",
    data: function () {
        return {
            loading:false,
            keyword: '',
            tableData: [],
            total: 0,
            pageSize: 10,
            pageIndex: 1,
            tableHeight: "",
            sels: [],
            visible: true,
            cols: [
                {prop: 'colOne', label: '列一'},
              {prop: 'colTwo', label: '列二'},
              {prop: 'colThr', label: '列三'},
              {prop: 'userAccount', label: '操作人账号'},
              {prop: 'oprDate', label: '操作时间'}
            ]
        }
    },
    methods: {
        search: function () {
            this.getData(1, this.pageSize, this.keyword);
        },
        editDic: function (row) {
        	is_add = false;
        	dialogInfos.dialogVisible = true;
        	dialogInfos.disabledInput = true;
        	dialogInfos.tableForm.colOne = row.colOne;
        	dialogInfos.tableForm.colTwo = row.colTwo;
        	dialogInfos.tableForm.colThr = row.colThr;
        	demoTableKey = row.demoTableKey;
        	dataType =  row.dataType;
        },        
        deleteDic: function(row){
        	msg = row;
        	this.$confirm('确认删除该所选字典信息？', '提示', {
        		confirmButtonText: '确定',
        		cancelButtonText: '取消',
        		type: 'warning'
        	}).then(function() {
        		VanyAjax("/demo/demotable/delete", {demoTableKey:msg.demoTableKey}, "GET").done(function (data) { 
        			sm.getData(sm.pageIndex,sm.pageSize,sm.keyword);
        	    }, true, this);
        	}).catch(function() {})
        },
        addDic: function () {
        	if(dataType==''){
        		this.$message('请选择数据类型！');
        	}else{
        		 dialogInfos.dialogVisible = true;
                 dialogInfos.disabledInput = false;
                 dialogInfos.tableForm.colOne = "";
             	dialogInfos.tableForm.colTwo = "";
             	dialogInfos.tableForm.colThr = "";
                 is_add = true;
        	}
        },
        getData: function (pageIndex, pageSize, keyword) {
        	var tbDataUrl = "/demo/demotable/queryList";
            VanyAjax(tbDataUrl, {
            	scrollPageIndex: pageIndex,
                scrollPageSize: pageSize,
                searchValue: keyword,
                dataType:dataType,
                token:sessionStorage.token
            }, "GET").done(function (data) { 
                this.currentPage = data.pageNum;
                this.pageSize = pageSize;
                this.total = data.total;
                var msgs = data.rows;
                debugger;
                for(var i =0;i<msgs.length;i++)
                {
                	var rowmsg = msgs[i];
                	rowmsg.oprDate = format(rowmsg.oprDate, 'yyyy-MM-dd HH:mm:ss');
                }
                this.tableData = msgs;
                
            }, false, this);
        },
        
        getCols: function (tablename) {
        	var tbDataUrl = "/demo/demotable/querycols";
            VanyAjax(tbDataUrl, {
            	tablename: tablename
            }, "GET").done(function (data) { 
            	this.cols = data;
            }, false, this);
        },

        handleCurrentChange: function (val) {
        	this.pageIndex = val;
            this.getData(val, this.pageSize, this.keyword);
        },

        handleSelectionChange: function (selection) {
            this.sels = selection;
        },
        
        handleSizeChange: function(val){
        	this.pageSize = val;
            this.getData(this.pageIndex, val, this.keyword);
        },
        dateFormat: function(row, column, cellValue) {
            var date = row[column.property];
            if(date == undefined) {
                return "";
            }
            date = format(date, 'yyyy-MM-dd HH:mm:ss');
            return date;
        }
    },
    mounted: function () {
    	var self = this;
    	this.$el.style.left = "176px";
		this.tableHeight = window.innerHeight - 320;
		this.getCols("demotable");
        this.getData(this.pageIndex, this.pageSize, this.keyword);
    },
})
